<template>
  <base-dialog
    :title="title + '邮箱账号'"
    ref="baseDialog"
    @handleClose="handleClose"
    @handleSubmit="handleSubmit"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="135px">
      <el-form-item label="邮箱" prop="mail">
        <el-input v-model="form.mail" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名，一般和邮箱一致" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="SMTP 服务器域名" prop="host">
        <el-input v-model="form.host" placeholder="请输入 SMTP 服务器域名" />
      </el-form-item>
      <el-form-item label="SMTP 服务器端口" prop="port">
        <el-input
          v-model="form.port"
          oninput="value=value.replace(/[^\d]/g,'')"
          placeholder="请输入 SMTP 服务器端口"
        />
      </el-form-item>
      <el-form-item label="是否开启 SSL" prop="sslEnable">
        <el-radio-group v-model="form.sslEnable">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </base-dialog>
</template>

<script>
import { add, edit, getById } from "@/api/message/mail/account"
import { checkEmail, validateDomain } from "@/utils/validateRules"

const ssl = {
  true: 1,
  false: 0
}
export default {
  name: "Dialog",
  data() {
    return {
      title: "",
      form: {},
      rules: {
        mail: [{ required: true, validator: checkEmail, trigger: "blur" }],
        username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
        host: [{ required: true, validator: validateDomain, trigger: "blur" }],
        port: [{ required: true, message: "SMTP 服务器端口不能为空", trigger: "blur" }],
        sslEnable: [{ required: true, message: "是否开启 SSL不能为空", trigger: "blur" }]
      }
    }
  },
  methods: {
    init(item, title) {
      this.title = title
      this.$refs.baseDialog.dialogVisible = true
      if (item) {
        // this.form = JSON.parse(JSON.stringify(item))
        getById(item.id).then((res) => {
          this.form = res.data
          this.form.sslEnable = ssl[res.data.sslEnable]
        })
      }
    },
    handleClose() {
      this.form = {}
      this.$refs.baseDialog.dialogVisible = false
      this.title = ""
    },
    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.title === "编辑") {
            try {
              await edit(this.form)
              this.$message({
                type: "success",
                message: "邮箱账号编辑成功"
              })
              this.$emit("updatePage")
              this.handleClose()
            } catch (error) {
              this.$message({
                type: "error",
                message: "邮箱账号编辑失败"
              })
            }
          } else {
            try {
              await add(this.form)
              this.$message({
                type: "success",
                message: "邮箱账号新增成功"
              })
              this.$emit("updatePage")
              this.handleClose()
            } catch (error) {
              this.$message({
                type: "error",
                message: "邮箱账号新增失败"
              })
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select,
.picker,
.el-input-number {
  width: 100%;
}
</style>
